package cn.northsea.controller;

import cn.northsea.common.CollaborativeFilteringJobRecommendation;
import cn.northsea.common.Result;
import cn.northsea.entity.Seek;
import cn.northsea.entity.SeekDz;
import cn.northsea.entity.SeekUser;
import cn.northsea.service.SeekService;
import cn.northsea.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.northsea.common.enums.ResultCodeEnum.REGISTER_SUCCESS;
import static cn.northsea.common.enums.ResultCodeEnum.USER_EXIST_ERROR;

@CrossOrigin
@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private SeekService seekService;
    @Autowired
    private UserService userService;
    @GetMapping("/getPage")
    //首页算法
    public Result index(@RequestParam(required = false,defaultValue = "")String likeName, SeekUser seekUser) {
        System.out.println(likeName);
        List<SeekUser> list = userService.list();
        HashMap<String, Map<String, Integer>> map = new HashMap<>();
        if (seekUser.getUsername()==null||seekUser.getUsername()=="")
        {
            List<Seek> seekList= seekService.findAll(likeName);
            return Result.success(seekList);
        }
        SeekUser user = userService.findJobByName(seekUser.getUsername());
        System.out.println(user);
        if (user==null)
        {
            List<Seek> seekList= seekService.findAll(likeName);
            return Result.success(seekList);
        }
        for (SeekUser seekUser1 : list)
        {
            if (!seekUser.getUsername().equals(seekUser1.getUsername()))
            {
                if(seekUser1.getJob()==null||seekUser1.getJob()=="")
                {
                    continue;
                }
                HashMap<String, Integer> map1 = new HashMap<>();
                map1.put(seekUser1.getJob(),(int)(Math.random()*4)+1);
                map.put(seekUser1.getUsername(),map1);
            }else {
                HashMap<String, Integer> map1 = new HashMap<>();
                map1.put(seekUser1.getJob(),5);
                map.put(seekUser1.getUsername(),map1);
            }
        }
//        map1.put(user.getJob(),5);
//        map.put(seekUser.getUsername(),map1);
        List<Map.Entry<String, Double>> recommendations = CollaborativeFilteringJobRecommendation.recommendJobs(seekUser.getUsername(),5,map);
        System.out.println(recommendations);
        int i=0;
        String [] list1 = new String[5];
        for (Map.Entry<String, Double> entry : recommendations)
        {
            list1[i]=entry.getKey();
            i++;
        }
        String s1 = list1[0];
        String s2 = list1[1];
        String s3 = list1[2];
        String s4 = list1[3];
        String s5 = list1[4];
        List<Seek> seekList = seekService.findAllByJob(user.getJob(),s1,s2,s3,s4,s5);
        return Result.success(seekList);
    }
    @GetMapping("/UserFindAll")
    //查询所有用户
    public Result index() {
        List<SeekUser> list = userService.findAll();
        return Result.success(list);
    }
    //添加个人信息
    @PostMapping("/addInformation")
    public Result addInformation(@RequestBody SeekUser user) {
        Date currentDate = new Date();
        user.setCreateTime(currentDate);
        userService.addInformation(user);
        return Result.success(REGISTER_SUCCESS);
    }
    @PostMapping("/findInformation")
    public Result findInformation(@RequestBody SeekUser user) {
        SeekUser seekUser = userService.findUserById(user.getUserId());
        return Result.success(seekUser);
    }
    @PostMapping("/register")
    public Result register(@RequestBody SeekUser user) {
        SeekUser seekUser = userService.findJobByName(user.getUsername());
        if (seekUser!=null)
        {
            return Result.error(USER_EXIST_ERROR);
        }
        Date currentDate = new Date();
        user.setCreateTime(currentDate);
        userService.register(user);
        return Result.success(REGISTER_SUCCESS);
    }
    @PostMapping("/update")
    public Result update(@RequestBody SeekUser user) {
        userService.updateById(user);
        return Result.success();
    }
    @PostMapping("/getUserById")
    public Result getUserById(@RequestBody SeekUser user) {
        SeekUser seekUser = userService.findUserById(user.getUserId());
        return Result.success(seekUser);
    }

    @GetMapping("/userfirend")
    public Result userfirend(@RequestParam Integer uid)
    {
        List<SeekDz> list= userService.userfirend(uid);
        return Result.success(list);
    }
}
